import AntModal from '@/components/pt/dialog/AntModal'
import { getBizProductCategory, addBizProductCategory, updateBizProductCategory, findMaxSort } from '@/api/biz/bizProductCategory'

export default {
  name: 'CreateForm',
  props: {
    categoryTypeOptions: {
      type: Array,
      required: true
    },
    bizProductCategoryOptions: {
      type: Array,
      required: true
    }
  },
  components: {
    AntModal

  },
  data () {
    return {
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      total: 0,
      disabled: false,
      id: undefined,
      formTitle: '添加产品分类',
      currentRow: undefined,
      oldParentId: '',
      // 表单参数
      form: {
        id: undefined,
        parentId: undefined
      },
      rules: {
        categoryName: [{ required: true, message: '分类名称不能为空', trigger: 'blur' }],

        categoryType: [{ required: true, message: '分类类型不能为空', trigger: 'blur' }]

      }
    }
  },
  filters: {},
  created () {},
  computed: {},
  watch: {},
  mounted () {},
  methods: {
    onClose () {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 表单重置
    reset () {
      this.form = {
        id: undefined,
        categoryName: undefined,

        treeSort: undefined,

        categoryType: undefined,

        remark: undefined

      }
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.$emit('select-tree')
      this.oldParentId = ''
      if (row != null && row.id) {
        this.currentRow = row
        this.oldParentId = row.id
        this.form.parentId = row.id
      } else {
        this.form.parentId = 0
      }
      /** 获取最大编号 */
      findMaxSort(this.form.parentId).then(response => {
        this.form.treeSort = response.data
        this.open = true
        this.formTitle = '添加产品分类'
      })
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      this.$emit('select-tree', row)
      this.open = true
      this.spinning = !this.spinning
      this.currentRow = row
      const bizProductCategoryId = row.id
      getBizProductCategory(bizProductCategoryId).then(response => {
        this.form = response.data
        this.oldParentId = response.data.parentId
        this.formTitle = '修改产品分类'
        this.spinning = !this.spinning
      })
    },
    /** 提交按钮 */
    submitForm: function (closeDialog) {
      this.closeDialog = closeDialog
      this.disabled = true
      this.$refs.form.validate(valid => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form))
          this.spinning = !this.spinning
          if (this.form.id !== undefined) {
            updateBizProductCategory(saveForm).then(response => {
              this.$message.success('更新成功', 3)
              if (this.oldParentId !== this.form.parentId) {
                // 如果修改父节点则刷新树
                this.$emit('ok')
              } else {
                this.setNodeData(response.data)
              }
              this.disabled = false
              this.cancel()
            })
          } else {
            addBizProductCategory(saveForm).then(response => {
              this.$message.success('新增成功', 3)
              if (this.oldParentId !== this.form.parentId) {
                // 如果修改父节点则刷新树
                this.$emit('ok')
              } else {
                this.appendTreeNode(this.currentRow, response.data)
              }
              this.disabled = false
              if (this.closeDialog) {
                this.cancel()
              } else {
                this.reset()
                this.$emit('select-tree')
                /** 获取最大编号 */
                findMaxSort(this.form.parentId).then(response => {
                  this.form.treeSort = response.data
                })
              }
            })
          }
        } else {
          this.disabled = false
          return false
        }
      })
    },
    back () {
      const index = '/biz/bizproductcategory/index'
      this.$router.push(index)
    },
    setNodeData (data) {
      this.currentRow.categoryName = data.categoryName
      this.currentRow.treeSort = data.treeSort
      this.currentRow.categoryType = data.categoryType
      this.currentRow.remark = data.remark
    }

  }
}
